package lib.checkrep;

public abstract class StringCheck {

  //check if str begins or ends with ch
  public static boolean BeginsOrEndsWithChar(String str, char ch) {
    if (str.length() == 0) return false;
    if (str.charAt(0) == ch) return true;
    if (str.charAt(str.length()-1) == ch) return true;
    return false;
  }

  //check if str contains only specified characters.
  public static boolean ContainsOnly(String str, String characters) {
    for (int i = 0; i < str.length(); ++i) {
      if (characters.contains("" + str.charAt(i))) continue;
      return false;
    }
    return true;
  }

  //check if string length is inclusively between
  public static boolean StringLengthInRange(String str, int lower_bound, int upper_bound) {
    return str.length() >= lower_bound && str.length() <= upper_bound;
  }

  //check if str contains X occurences of ch, where X is inclusively between
  public static boolean StringContainsAtLeastX(String str, char ch, int lower_bound, int upper_bound) {
    int occ = 0;
    for (int i = 0; i < str.length(); ++i) {
      if (str.charAt(i) == ch) ++occ;
    }
    return occ >= lower_bound && occ <= upper_bound;
  }
  
  // check if str contains X occurrences of any characters in alphabet.
  public static boolean StringContainsXOccurrencesOf(
      String str, String alphabet, int lower_bound, int upper_bound) {
    int occ = 0;
    for (int i = 0; i < str.length(); ++i) {
      boolean ok = false;
      for (int j = 0; j < alphabet.length(); ++j) {
        if (alphabet.charAt(j) == str.charAt(i)) {
          ok = true;
        }
      }
      if (ok) ++occ;
    }
    return occ >= lower_bound && occ <= upper_bound;
  }

  /**
   * Checks if all the characters in the string are distinct.
   */
  public static boolean StringContainsDistinctCharacters(String str) {
    for (int i = 0; i < str.length(); ++i) {
      for (int j = i+1; j < str.length(); ++j) {
        if (str.charAt(i) == str.charAt(j)) return false;
      }
    }
    return true;
  }
};
